package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.allparentsmchBean;
import com.ymatou.iapi.godmch.parameter.bindmchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.parentInfo;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.allparentsmchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*获取商户所有上级商户身份
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_allparentsmch{
	private static allparentsmchBean bean;
	private static allparentsmchCall call;
	private static mch_infoWapper mchinfoWapper=new mch_infoWapper();
	private static mch_relationWapper relationWapper=new mch_relationWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户所有上级商户身份");
	}

	@Before
	public void caseUp() {
		bean=new allparentsmchBean();
		call=new allparentsmchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_allparentsmch_001() {
		Logger.start(true,"获取商户所有上级商户身份,验证传入正确商户id,当前商户只有一个父级，代理模式 默认 1自主选货-返回成功");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			call.setData(bean);
			call.callService();			
			
			ArrayList list=new ArrayList();
			list.add(userId_channl);
			//验证点
			checkverify(list,userId_agent);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_allparentsmch_001_2() {
		Logger.start(true,"获取商户所有上级商户身份,验证传入正确商户id,当前商户只有一个父级，代理模式2.自动继承-返回成功");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			//createmchbean.setCountry("CHN");
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			
			CreateCodeBean createCodeBean = GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			
			createCodeBean.setAgentModel(2);//2.自动继承
			CreateCodeCall createCall = GodmchManageCallService.createCodeCall(createCodeBean);
			
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_agent);
			call.setData(bean);
			call.callService();			
			
			ArrayList list=new ArrayList();
			list.add(userId_channl);
			//验证点
			checkverify(list,userId_agent);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_allparentsmch_002() {
		Logger.start(true,"获取商户所有上级商户身份,验证一个商户id同时有两个代理渠道,返回成功");
		try {
			//渠道1
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl1=DGUserCallService.UserCreateCall(usercreateBean1);
			//渠道2
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl2=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			//创建渠道
			Logger.comment("创建渠道1");
			createmchBean createmchbean1=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl1);
			GodmchManageCallService.createmchCallForChannel(createmchbean1);
			List<createmchBean> createmchList1=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl1, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList1.toArray(new
					createmchBean[createmchList1.size()]));
			//创建渠道2
			Logger.comment("创建渠道2");
			createmchBean createmchbean2=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl2);
			GodmchManageCallService.createmchCallForChannel(createmchbean2);
			//渠道2 创建邀请码
			CreateCodeBean createcodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl2, true);
			CreateCodeCall createCodeCall=GodmchManageCallService.createCodeCall(createcodeBean);
			//渠道2绑定代理
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(userId_agent, createCodeCall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			
			bean.setMchId(userId_agent);
			call.setData(bean);
			call.callService();			
			
			ArrayList list=new ArrayList();
			list.add(userId_channl1);
			list.add(userId_channl2);
			//验证点
			checkverify(list,userId_agent);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//自动继承 只能有一个上级渠道，此case 不可能发生
	public void Tc_allparentsmch_002_1() {
		Logger.start(true,"获取商户所有上级商户身份,验证一个商户id同时有两个代理渠道,其中一渠道是1.自主选货，另外一渠道2.自动继承 返回成功");
		try {
			//渠道1
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl1=DGUserCallService.UserCreateCall(usercreateBean1);
			//渠道2
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl2=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			//创建渠道
			Logger.comment("创建渠道1");
			createmchBean createmchbean1=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl1);
			GodmchManageCallService.createmchCallForChannel(createmchbean1);
			
			CreateCodeBean createCodeBean = GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl1);
			createCodeBean.setAgentModel(2);//1.自主选货
			CreateCodeCall createCall = GodmchManageCallService.createCodeCall(createCodeBean);
			
			List<createmchBean> createmchList1=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl1, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList1.toArray(new
					createmchBean[createmchList1.size()]));
			//创建渠道2
			Logger.comment("创建渠道2");
			createmchBean createmchbean2=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl2);
			GodmchManageCallService.createmchCallForChannel(createmchbean2);
			//渠道2 创建邀请码
			CreateCodeBean createcodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl2, true);
			createcodeBean.setAgentModel(1);//2.自动继承
			CreateCodeCall createCodeCall=GodmchManageCallService.createCodeCall(createcodeBean);
			//渠道2绑定代理
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(userId_agent, createCodeCall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			
			bean.setMchId(userId_agent);
			call.setData(bean);
			call.callService();			
			
			ArrayList list=new ArrayList();
			list.add(userId_channl1);
			list.add(userId_channl2);
			//验证点
			checkverify(list,userId_agent);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_allparentsmch_003() {
		Logger.start(true,"获取商户所有上级商户身份,验证商户id不存在,返回失败,提示错误");
		try {
			
			bean.setMchId(9999);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("200", call.getStatus1(), "验证status");
			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message(");
			Logger.verifyEquals(0, call.getAllParentsList().size(), "验证返回parents为空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_allparentsmch_004() {
		Logger.start(true,"获取商户所有上级商户身份,验证商户id为空,返回失败,提示错误");
		try {
			bean.setMchId(null);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatus1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("商户id不能为空"), "验证Message不为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
    /**
     * 断言
     * @param userId_channl 渠道ID，也就是父级id
     * @param userId_agent  代理Id ,下级id
     * @param parentCount   有多少个父级
     * @throws InterruptedException
     */
	private void checkverify(ArrayList userId_channl,int userId_agent) throws InterruptedException
	{
		
		Logger.verifyEquals("200", call.getStatus1(), "验证status");
		Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
		
		//商户关系 根据mchid 查询所有父级
        HashMap<String,Object> map=new HashMap();
        map.put("mchId", userId_agent);
        List<mch_relation> relationList=relationWapper.selectby(map);
        
		List<parentInfo> return_pList=call.getAllParentsList();
		Logger.verifyEquals(userId_channl.size(), return_pList.size(), "验证返回有"+userId_channl.size()+"个父级");
		Logger.verifyEquals(relationList.size(), return_pList.size(), "验证返回的父级与关系表mch_relation一致");

		for(int i=0;i<return_pList.size();i++)
		{
			parentInfo return_p=return_pList.get(i);
		    Logger.verifyEquals(return_p.getParentMchId(), relationList.get(i).getParentMchId(), "验证数据库mch_relation中的ParentMchId与返回的是否一致");
			//上级商户
			HashMap<String,Object> map2=new HashMap();
			map2.put("mchId", return_p.getParentMchId());
			mch_info p_mch= mchinfoWapper.selectby(map2).get(0);
			//mch_info
			Logger.verifyEquals(return_p.getAgent(), p_mch.getAgent()==1?true:false, "验证数据库mch_info中的agent与返回的是否一致");
	        Logger.verifyEquals(return_p.getChannel(), p_mch.getChannels()==1?true:false, "验证数据库mch_info中的Channel与返回的是否一致");
	        Logger.verifyEquals(return_p.getVip(), p_mch.getVip()==1?true:false, "验证数据库mch_info中的Vip与返回的是否一致");
	        Logger.verifyEquals(return_p.getParentMchId(), userId_channl.get(i), "验证返回的userId_channl是否一致");
	        
	        Logger.verifyEquals(return_p.getMchTitle(), p_mch.getMchTitle()==""?p_mch.getNickName():p_mch.getMchTitle(), "验证数据库mch_info中的MchTitle与返回的是否一致");
             //mch_relation
	        Logger.verifyEquals(return_p.getAgentType(), relationList.get(i).getAgentType(), "验证数据库mch_relation中的AgentType与返回的是否一致");
	        Logger.verifyEquals(return_p.getEnableSubAgent(), relationList.get(i).getAddSubAgent()==1?true:false, "验证数据库mch_relation中的EnableSubAgent与返回的是否一致");
	        
	        Logger.verifyEquals(return_p.getAgentModel()==0?1:return_p.getAgentModel(), relationList.get(i).getAgentModel(), "验证数据库mch_info中的AgentModel与返回的是否一致");
	        //agentModelText
	    	if(return_p.getAgentModel()==1)
	           Logger.verifyEquals("自主选货",return_p.getAgentModelText(), "验证AgentModelText与返回的是否一致");
	    	else if(return_p.getAgentModel()==2)
	    		Logger.verifyEquals("自动继承",return_p.getAgentModelText(), "验证AgentModelText与返回的是否一致");
		}
	}
}